﻿using Microsoft.Playwright;

// https://playwright.net.cn/dotnet

var playwright = await Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
    Headless = false,
    SlowMo = 500  // 步骤之间执行的延迟
});

var page = await browser.NewPageAsync();

await page.GotoAsync("http://localhost:5266/");

// 操作元素
// 获取文本框并填入内容
// input[type=text|email|number] ，textarea
// Locator.Fill
await page.Locator("#exampleFormControlInput1").FillAsync("perter");

// 逐字符地键入到字段中，就像是用户使用真实的键盘一样。
await page.Locator("#exampleFormControlInput2").PressSequentiallyAsync("Hello world", new()
{
    Delay = 200
});

// 其他按键
//// Hit Enter
//await page.GetByText("Submit").PressAsync("Enter");
// Dispatch Control+Right
await page.Locator("#exampleFormControlInput2").PressAsync("Control+A");
//// Press $ sign on keyboard
//await page.GetByRole(AriaRole.Textbox).PressAsync("$");


// input[type=checkbox|radio]
// 获取 checkbox 并勾上，不论元素初始时是否勾上
// Locator.Check
await page.GetByLabel("Default checkbox").CheckAsync();
// 获取 radio 并勾上
await page.GetByLabel("Default radio").CheckAsync();

// select
// Locator.SelectOptionAsync
await page.GetByLabel("Default select example").SelectOptionAsync("Three");


// Generic click
//await page.GetByRole(AriaRole.Button).ClickAsync();
await page.Locator("#primary").ClickAsync();

// Double click
//await page.GetByText("Item").DblClickAsync();
await page.Locator("#exampleFormControlInput1").DblClickAsync();

// Right click
//await page.GetByText("Item").ClickAsync(new() { Button = MouseButton.Right });
await page.Locator("#exampleFormControlInput1").ClickAsync(new()
{
    Button = MouseButton.Right
});

//// Shift + click
//await page.GetByText("Item").ClickAsync(new() { Modifiers = new[] { KeyboardModifier.Shift } });

//// Ctrl + click on Windows and Linux
//// Meta + click on macOS
//await page.GetByText("Item").ClickAsync(new() { Modifiers = new[] { KeyboardModifier.ControlOrMeta } });

// Hover over element
//await page.GetByText("Item").HoverAsync();
await page.Locator("#danger").HoverAsync();

//// Click the top left corner 精准点击
//await page.GetByText("Item").ClickAsync(new() { Position = new Position { X = 0, Y = 0 } });

Console.ReadKey();